        var itemStore = new Ext.data.JsonStore({
			url: '/weihe/getItems_inventoryList',
			root: 'datas',
			fields : [{
			        	   name:'itemId',mapping:'id'
			          },{
			        	   name:'itemCount',mapping:'count' 
			          },{
			        	   name:'itemPLCount',mapping:'plCount' 
			          },{
			        	   name:'itemCheckCount',mapping:'checkCount' 
			          },{
			        	   name:'itemSum',mapping:'sum'
			          },{
			        	   name:'itemCheckSum',mapping:'checkSum'
			          },{
			        	   name:'itemPLSum',mapping:'plSum'
        			  },{
        				   name:'cargoId',mapping:'cargo', convert:function(v){ 
														return v ? v.id : undefined;}
        			  },{
        				   name:'itemPrice',mapping:'cargo', convert:function(v){ 
														return v ? v.price : undefined;}
        			  },{
       				   	   name:'itemLocation',mapping:'cargo', convert:function(v){ 
														return v ? v.location : undefined;}
        			  },{
						   name:'inventoryCode',mapping:'cargo', convert:function(v){ 
														return v ? v.code : undefined;}
			          },{
			        	   name:'inventoryName',mapping:'cargo', convert:function(v){ 
														return v ? v.name : undefined;}
			          },{
			        	   name:'itemSpecs',mapping:'cargo', convert:function(v){ 
			        	  								return v ? v.specs : undefined;}
			          },{
			        	   name:'itemUnit',mapping:'cargo', convert:function(v){ 
			        	  								return v ? v.unit : undefined;}
			          }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_inventoryList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}

		});
        
        var inventoryListStore = new Ext.data.JsonStore({
			url: '/weihe/getList_inventoryList',
			root: 'datas',
			fields : [{
			        	   name:'inventoryListId',mapping:'id'
			          },{
        				   name:'inventoryListNum',mapping:'listNum'
        			  },{
        				   name:'inventoryDate', mapping:'date',type : 'date', dateFormat:"Y-m-d"
        			  },{
        				   name:'inventoryDepartment',mapping:'department'
        			  },{
        				   name:'inventoryInType',mapping:'inType'
        			  },{
        				   name:'inventoryOutType',mapping:'outType'
        			  },{
        				   name:'inventoryRemark',mapping:'remark'
        			  },{
        				   name:'inventoryHandler',mapping:'handler'
        			  },{
        				   name:'inventoryWarehouse',mapping:'warehouse'
        			  },{
        				   name:'inventoryMaker',mapping:'maker'
        			  },{
        				   name:'inventoryAuditor',mapping:'auditor'
        			  }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_inventoryList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}

		});
    	
    	
    
    	

inventorys = function(){
  return{
	  
	  listPanel : function(){
	  
		var listPanel = new Ext.Panel( {
			                    region : 'north',
			                    layout : 'border',
			                    frame  : true,
			                    height : 230,
			                    items  : [inventorys.listGrid(),inventorys.listForm()]
		                      });
	  
	  return listPanel;
  	},

  	  listForm : function(){
	  
		var button_add = new Ext.Button({
			text : '添加',
			handler : function() {
				progressConsole();
				if(listForm.form.isValid()){ 
					Ext.getCmp("inventorysForm").buttons[0].enable();
					Ext.getCmp("inventorysForm").buttons[1].enable();
					Ext.getCmp("inventorysForm").buttons[2].enable();
					var tmp = listForm.form.findField('inventoryList.warehouse').getValue();
					Ext.getCmp('inventoryCode').getStore().setBaseParam('location',tmp);
					Ext.getCmp('inventoryCode').getStore().load({params : {query : '%'}});
					Ext.getCmp('inventoryName').getStore().setBaseParam('location',tmp);
					Ext.getCmp('inventoryName').getStore().load({params : {query : '%'}});
					if (listForm.form.findField("inventoryListId").getValue() == "") {
						 listForm.form.submit({
						     url : '/weihe/add_inventoryList',
						     success : function(form, action) {
							      if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
								        Ext.Msg.alert('消息', action.result.msg, function() {
								        listForm.form.reset();
								        Ext.getCmp("inventoryGrid").getStore().removeAll();
									    Ext.getCmp("inventorysForm").form.findField('inventoryList.id').setValue(action.result.inventoryList_id);
									    Ext.getCmp("inventoryListGrid").getStore().load({params : {start : action.result.total-1, limit : 5}});;
								        })
							       }
						     },
							 failure : function(form, action) {
	      							Ext.Msg.alert('错误', action.result.msg);
	      						}
					      })
					}else {
							listForm.form.submit({
								url : '/weihe/update_inventoryList',
								success : function(form, action) {
									 if (action.result.deny) {
									   Ext.Msg.alert('错误', action.result.msg);
								   }else{
										Ext.Msg.alert('消息', action.result.msg, function() {
											Ext.getCmp("inventoryListGrid").getStore().load({params : {start : 0, limit : 5}});
										})
									}
							   },
								 failure : function(form, action) {
	      							Ext.Msg.alert('错误', action.result.msg);
	      						}
							})
					}
				}else{
					Ext.Msg.alert('提示','请输入相关内容');
				}
			}
		});

	var button_clear = new Ext.Button({
		text : '清空',
		handler : function() {
		
			listForm.form.reset();
			Ext.getCmp("inventorysForm").form.reset();
			listForm.buttons[0].setText('添加');
			Ext.getCmp("inventorysForm").buttons[0].disable();
			Ext.getCmp("inventorysForm").buttons[1].disable();
			Ext.getCmp("inventorysForm").buttons[2].disable();
			Ext.getCmp("inventoryGrid").getStore().removeAll();
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		handler : function() {
			var id = listForm.form.findField('inventoryListId').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/delete_inventoryList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									listForm.form.reset();
								Ext.getCmp("inventorysForm").buttons[0].disable();
								Ext.getCmp("inventorysForm").buttons[1].disable();
								Ext.getCmp("inventorysForm").buttons[2].disable();
								listForm.buttons[0].setText('添加');
						  }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("inventoryListGrid").getStore().load({params : {start : 0, limit : 5}});
								listForm.form.reset();
								Ext.getCmp("inventorysForm").buttons[0].disable();
								Ext.getCmp("inventorysForm").buttons[1].disable();
								Ext.getCmp("inventorysForm").buttons[2].disable();
								listForm.buttons[0].setText('添加');
							});
						  }
						}else{
							Ext.Msg.alert('消息', json.msg);
						}
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'inventoryList.id' : listForm.form.findField('inventoryListId').getValue() }
				});
			}
		}
	});
	
	  
	  var whComboData = new Ext.data.SimpleStore({
			fields : ['value','text'],
			data : [['A','A'],['B','B'],['C','C'],['报废','报废']]
		});
	  
			var listForm = new Ext.form.FormPanel({
			id : 'inventoryListForm',
			title : '盘点信息',
			frame : true,
			region : 'east',
			width : 300,
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{  // Ext.form.Hidden
				id: 'inventoryListId',
				xtype : 'hidden',  // 隐藏的表单
				name : 'inventoryList.id'
			},{
				id: 'inventoryListNum',
				fieldLabel: "盘点单号",
        	    name: "inventoryList.listNum"
			},{
				id: 'inventoryDate',
              xtype: 'datefield',
              fieldLabel: "盘点日期", //第三行，出生日期 
              format : 'Y-m-d',
  			allowBlank:false,
              name: "inventoryList.date"
			},{
				id: 'inventoryOutType',
				 fieldLabel: "出库类别",
              name: "inventoryList.outType"
			},{
				id: 'inventoryInType',
				 fieldLabel: "入库类别",
               name: "inventoryList.inType"
			},{
				id:'inventoryWarehouse',
				fieldLabel : '盘点仓库',
				hiddenName : 'inventoryList.warehouse',
				xtype : 'combo',
				store : whComboData,
				emptyText : '请选择',
				mode : 'local',
				triggerAction : 'all',
				allowBlank:false,
				editable: false,
				valueField : 'value',
				displayField : 'text'
			},{
				id: 'inventoryDepartment',
				 fieldLabel: "部门",
             name: "inventoryList.department"
			},{
				id: 'inventoryHandler',
				 fieldLabel: "经手人",
            name: "inventoryList.handler"
			},{
				id: 'inventoryRemark',
				 fieldLabel: "备注",
              name: "inventoryList.remark"
			},{
				id: 'inventoryMaker',
				 fieldLabel: "制单人",
             name: "inventoryList.maker"
			}],
			buttons : [button_add,button_clear,button_del]
	});
	  
	  return listForm;
},  
	  
	  
listGrid : function(){
	  
		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 5,
			store : inventoryListStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var listGrid = new Ext.grid.GridPanel({
		   id:'inventoryListGrid',
		   region: 'center',
		   minColumnWidth : 35,
		   loadMask : {msg:'正在加载数据，请稍侯...'},
		   store : inventoryListStore,
		   columns: [
		             {header : '盘点单号', dataIndex : 'inventoryListNum'},
		             {header : '盘点日期', dataIndex : 'inventoryDate',sortable : true,renderer:Ext.util.Format.dateRenderer('Y年m月d日')},
		             {header : '出库类别', dataIndex : 'inventoryOutType', menuDisabled : true},
		             {header : '入库类别', dataIndex : 'inventoryInType', menuDisabled : true},
		             {header : '仓库', dataIndex : 'inventoryWarehouse', menuDisabled : true},
		             {header : '部门', dataIndex : 'inventoryDepartment', menuDisabled : true},
		             {header : '经手人', dataIndex : 'inventoryHandler', menuDisabled : true},
		             {header : '备注', dataIndex : 'inventoryRemark', menuDisabled : true},
		             {header : '制单人', dataIndex : 'inventoryMaker', menuDisabled : true},
		             {header : '审核人', dataIndex : 'inventoryAuditor', menuDisabled : true}
		             ],
       sm : rowSelectionModel,
       listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   		var record = grid.getStore().getAt(rowIndex);
			   		Ext.getCmp("inventoryListForm").form.loadRecord(record);
			   		Ext.getCmp("inventorysForm").form.findField('inventoryList.id').setValue(record.id);
			   		Ext.getCmp("inventorysForm").buttons[0].enable();
					Ext.getCmp("inventorysForm").buttons[1].enable();
					Ext.getCmp("inventorysForm").buttons[2].enable();
					Ext.getCmp("inventoryListForm").buttons[0].setText('修改');
					Ext.getCmp('inventoryCode').getStore().setBaseParam('location',record.get('inventoryWarehouse'));
					Ext.getCmp('inventoryCode').getStore().load({params : {query : '%'}});
					Ext.getCmp('inventoryName').getStore().setBaseParam('location',record.get('inventoryWarehouse'));
					Ext.getCmp('inventoryName').getStore().load({params : {query : '%'}});
					Ext.getCmp("inventoryGrid").getStore().baseParams = {'inventoryList.id': record.id};
					Ext.getCmp("inventoryGrid").getStore().load({params : {start : 0, limit : 5}});
		   		}
		   },
		  bbar : pageToolBar
});
		return listGrid;
	},
	  
	  
	  
	  
	  
	  
	  
	  
	  
	
    grid : function(){
	 


		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 5,
			store : itemStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		   


		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var grid = new Ext.grid.GridPanel({
		   id:'inventoryGrid',
		   title : '货物信息表',
		   region : 'center',
		   minColumnWidth : 50,
		   loadMask : {msg:'正在加载数据，请稍侯...'},
		   store : itemStore,
		   columns: [
		             {header : '产品编号', dataIndex : 'inventoryCode'},
		             {header : '产品名称', dataIndex : 'inventoryName'},
		             {header : '规格型号', dataIndex : 'itemSpecs'},
		             {header : '单位', dataIndex : 'itemUnit'},
		             {header : '单价', dataIndex : 'itemPrice'},
		             {header : '账面数量', dataIndex : 'itemCount'},
		             {header : '盘点数量', dataIndex : 'itemCheckCount'},
		             {header : '盈亏数量', dataIndex : 'itemPLCount'},
		             {header : '账面金额', dataIndex : 'itemSum'},
		             {header : '盘点金额', dataIndex : 'itemCheckSum'},
		             {header : '盈亏金额', dataIndex : 'itemPLSum'}
        ],
           sm : rowSelectionModel,
           listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   var record = grid.getStore().getAt(rowIndex);
			   Ext.getCmp("inventorysForm").form.loadRecord(record);
			   Ext.getCmp("inventorysForm").buttons[0].setText('修改');
			  
		   }
		},
		  viewConfig : {
		     forceFit : false
		  },
		  bbar : pageToolBar
});
		return grid;
	},
	inventorysForm : function(){
		
		var codestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByCode_depotList',
			root: 'codes',
			totalProperty: 'count',
			fields: ['code']
		});
		
		var namestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByname_depotList',
			root: 'names',
			totalProperty: 'count',
			fields: ['name']
		});
		
		var codeCombo = {
				id : 'inventoryCode',
				xtype: 'combo',
				fieldLabel: '产品编号',
				store: codestore,
				typeAhead: false,
				name : 'cargo.code',
				loadingText: '查找中...',
				emptyText: '请输入3位以上编号', 
				minChars: 3,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'code',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getCode_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  inventorysForm.form.findField('cargo.name').setValue(json.name);
									  inventorysForm.form.findField('cargo.specs').setValue(json.specs);
									  inventorysForm.form.findField('cargo.unit').setValue(json.unit);
									  inventorysForm.form.findField('cargo.price').setValue(json.price);
									  inventorysForm.form.findField('cargo.count').setValue(json.count);
									  inventorysForm.form.findField('cargo.id').setValue(json.id);
									},
								params : 'query=' + record.get('code')
							});
				    }
				}
		}
		var nameCombo = {
				id : 'inventoryName',
				xtype: 'combo',
				fieldLabel: '产品名称',
				store: namestore,
				typeAhead: false,
				name : 'cargo.name',
				loadingText: '查找中...',
				emptyText: '请输入产品名称', 
				minChars: 2,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'name',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getName_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  inventorysForm.form.findField('cargo.name').setValue(json.name);
									  inventorysForm.form.findField('cargo.specs').setValue(json.specs);
									  inventorysForm.form.findField('cargo.unit').setValue(json.unit);
									  inventorysForm.form.findField('cargo.price').setValue(json.price);
									  inventorysForm.form.findField('cargo.count').setValue(json.count);
									  inventorysForm.form.findField('cargo.id').setValue(json.id);
									},
								params : 'query=' + record.get('name')
							});
				    }
				}
		}

		var button_add = new Ext.Button({
			text : '添加',
			disabled : true,
			handler : function() {

					progressConsole();
				if(inventorysForm.form.isValid()){ 	
					if (inventorysForm.form.findField("item.id").getValue() == "") {
						inventorysForm.form.submit({
						  url : '/weihe/addItem_inventoryList',
						   success : function(form, action) {
							  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
								Ext.Msg.alert('消息', action.result.msg, function() {
									 var i = inventorysForm.form.findField('inventoryList.id').getValue();
									   inventorysForm.form.reset();
									   inventorysForm.form.findField('inventoryList.id').setValue(i);
									   Ext.getCmp("inventoryGrid").getStore().baseParams = {'inventoryList.id': i};
									   Ext.getCmp("inventoryGrid").getStore().load({params : {start : 0, limit : 5}});
									
								})
							  }
						    },
							 failure : function(form, action) {
      							Ext.Msg.alert('错误', action.result.msg);
      						}
					    })
					}else {
						inventorysForm.form.submit({
							  url : '/weihe/updateItem_inventoryList',
							   success : function(form, action) {
								  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										var i = inventorysForm.form.findField('inventoryList.id').getValue();
										inventorysForm.form.reset();
										 inventorysForm.form.findField('inventoryList.id').setValue(i);
										Ext.getCmp("inventoryGrid").getStore().reload();
										    inventorysForm.buttons[0].setText('添加');
										
									})
								  }
							    },
							    failure : function(form, action) {
	      							Ext.Msg.alert('错误', action.result.msg);
	      						}
						    })

				
						
					}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
				}
		
			
		});

	var button_clear = new Ext.Button({
		text : '清空',
		disabled : true,
		handler : function() {
		var i = inventorysForm.form.findField('inventoryList.id').getValue();
			inventorysForm.form.reset();
			inventorysForm.form.findField('inventoryList.id').setValue(i);
			inventorysForm.buttons[0].setText('添加');
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		disabled : true,
		handler : function() {
			var id = inventorysForm.form.findField('item.id').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/deleteItem_inventoryList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						   if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									var i = inventorysForm.form.findField('inventoryList.id').getValue();
								inventorysForm.form.reset();
								inventorysForm.form.findField('inventoryList.id').setValue(i);
								inventorysForm.buttons[0].setText('添加');
						   }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("inventoryGrid").getStore().load({params : {start : 0, limit : 5}});
								var i = inventorysForm.form.findField('inventoryList.id').getValue();
								inventorysForm.form.reset();
								inventorysForm.form.findField('inventoryList.id').setValue(i);
								inventorysForm.buttons[0].setText('添加');
							});
						}
					  }
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'item.id': id , 'inventoryList.id' : inventorysForm.form.findField('inventoryList.id').getValue() }
				});
			}
		}
	});
	
		var inventorysForm = new Ext.form.FormPanel({
			id : 'inventorysForm',
			title : '货物信息',
			frame : true,
			width : 300,
			region : 'east',
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{ 
				id: 'itemId',
				xtype : 'hidden',
				name : 'item.id'
			},{  
				xtype : 'hidden', 
				name : 'inventoryList.id'
			},{ 
				xtype : 'hidden',
				name : 'cargo.id'
			},codeCombo ,nameCombo
			 ,{
				id:'itemSpecs',
				fieldLabel : '规格型号',
				name : 'cargo.specs',
				readOnly: true
			},{
				id:'itemUnit',
				fieldLabel : '单位',
				name : 'cargo.unit',
				readOnly: true
			},{
				id:'itemPrice',
				fieldLabel : '单价',
				name : 'cargo.price',
				readOnly: true
			},{
				id:'itemCount',
				fieldLabel : '数量',
				name : 'cargo.count',
				readOnly: true
			},{
				id:'itemCheckCount',
				fieldLabel : '盘点数量',
				name : 'item.checkCount',
				allowBlank:false,
				blankText:'请输入盘点数量',
				regex : /^[1-9]\d*$/,
			    regexText:'请输入有效的数量'  
			}],
			buttons : [button_add,button_clear,button_del]
		
	});
		return inventorysForm;
	}
  }
}();








